package com.andyage.util;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
 * 
 * @author shuai
 *
 */
public class DateUtil {

	private static final String DATE_FULL = "yyyy-MM-dd HH:mm:ss";
	private final static String DATE_YEAR_MONTH = "yyyy-MM";
	private final static String DATE_YEAR_MONTH_DAY = "yyyy-MM-dd";
	private final static String DATE_YEAR_MONTH_DAY_H_M = "yyyy-MM-dd HH:mm";

	public static Date getInitializedDate() {
		return getDate("1900-01-01 00:00:00");
	}

	public static Date getNeverReachDate() {
		return getDate("3000-01-01 00:00:00");
	}

	public static Date getDate(String date) {
		return DateUtil.getDate(date, DATE_FULL);
	}
	
	/**
	 * String-->date
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static Date getDate(String date, String pattern) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
			return simpleDateFormat.parse(date);
		} catch (ParseException e) {
		}
		return null;
	}
	/**
	 * 格式化时间date-->date
	 * @param date
	 * @param pattern
	 * @return Date
	 */
	public static Date getDate(Date date, String pattern) {
		try {
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
			String datestr=simpleDateFormat.format(date);
			return simpleDateFormat.parse(datestr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
//	public static int getDateDiff(Date d1,Date d2){
//	    d1=getDate(d1,DATE_YEAR_MONTH_DAY);
//	    d2=getDate(d2,DATE_YEAR_MONTH_DAY);
//
//	    long diff = d1.getTime() - d2.getTime();
//	    Long days = Math.abs(diff / (24 * 60 * 60 * 1000));
//	    return days.intValue();
//	}
	public static int getDateDiff(Date d1,Date d2){
		d1 = set2beEndOfDay(d1);
		d2 = set2beEndOfDay(d2);

		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar1.setTime(d1);
		calendar2.setTime(d2);

		Long t1 = calendar1.getTimeInMillis();
		Long t2 = calendar2.getTimeInMillis();
		int a = 24 * 60 * 60 * 1000;
		BigDecimal b1 = new BigDecimal(t1.toString());
		BigDecimal b2 = new BigDecimal(t2.toString());
		BigDecimal ba = new BigDecimal(String.valueOf(a));

		BigDecimal distance = b1.subtract(b2).divide(ba, RoundingMode.HALF_UP);
		if (distance.compareTo(BigDecimal.ZERO) > 0
				&& distance.compareTo(BigDecimal.ONE) < 0)
			return 1;
		if (distance.compareTo(BigDecimal.ONE.negate()) > 0
				&& distance.compareTo(BigDecimal.ZERO) < 0)
			return -1;

		// +1
		Long val = distance.longValue();
		if (distance.compareTo(new BigDecimal(val.toString())) > 0)
			val += 1;
		return  Math.abs(val.intValue());
	}

	public static Date getLastDateOfMonth(Date time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.MONTH, 1);
		calendar.add(Calendar.DATE, -1);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}

	public static Date getFirstDateOfMonth(Date time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	// 日期：yyyy-MM-dd HH:mm:ss
	public static String getDateFull(Date date) {
		return getDateString(date, DATE_FULL);
	}

	// yyyy-MM
	public static String getDateYearMonth(Date date) {
		return getDateString(date, DATE_YEAR_MONTH);
	}

	// yyyy-MM-dd
	public static String getDateYearMonthDay(Date date) {
		return getDateString(date, DATE_YEAR_MONTH_DAY);
	}

	// yyyy-MM-dd HH:mm
	public static String getDateYearMonthDayHourMin(Date date) {
		return getDateString(date, DATE_YEAR_MONTH_DAY_H_M);
	}
	/**
	 * date转String
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getDateString(Date date, String pattern) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
		return simpleDateFormat.format(date);
	}

	/**
	 * Add some days to a date.
	 *
	 * @param beginningDate
	 * @param days2add
	 * @return
	 */
	public static Date addDays(Date beginningDate, int days2add) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginningDate);
		calendar.add(Calendar.DATE, days2add);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Date addYear(Date beginningDate, int year2add) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginningDate);
		calendar.add(Calendar.YEAR, year2add);
		return calendar.getTime();
	}

	public static Date addHour(Date beginningDate, int hours) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginningDate);
		calendar.add(Calendar.HOUR, hours);
		return calendar.getTime();
	}

	public static Date addMonth(Date beginningDate, int monthes) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginningDate);
		calendar.add(Calendar.MONTH, monthes);
		return calendar.getTime();
	}

	public static Date addMin(Date beginningDate, int mins) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginningDate);
		calendar.add(Calendar.MINUTE, mins);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	public static Date addSec(Date beginningDate, int sec) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(beginningDate);
		calendar.add(Calendar.SECOND, sec);
		calendar.set(Calendar.MILLISECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 返回两个时间点的差值，单位（毫秒）
	 *
	 * @param dueDate
	 * @return difference in seconds
	 */
	public static long getRemainSeconds(Date nowDate, Date dueDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(nowDate);
		long now = calendar.getTimeInMillis();
		calendar.setTime(dueDate);
		long target = calendar.getTimeInMillis();// 获取毫秒
		if (now >= target)
			return 0;
		else
			return (target - now) / 1000;// 返回秒
	}

	/**
	 * 判断一个 日期 是否在某个时间段之内
	 *
	 * @param begin
	 *            开始时间
	 * @param end
	 *            结束时间
	 * @param contais
	 *            被包含的时间
	 * @return
	 */
	public static boolean beginEndDateContais(Date begin, Date end, Date contais) {
		Long b = begin.getTime();
		Long e = end.getTime();
		Long c = contais.getTime();

		if (b <= c && e >= c) {
			return true;
		}
		return false;
	}

	/**
	 * 日期格式化
	 *
	 * @param date
	 * @param format
	 * @return String类型的字符串
	 */
	public static String dateFormat(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}

	/**
	 * 获取月份中的天
	 * @param date
	 * @return
	 */
	public static int getDayOfMonth(Date date){
		if(date != null){
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar.get(Calendar.DAY_OF_MONTH);
		}
		return 0;
	}

	/**
	 * 把时间格式到一天的最后
	 * @param time
	 * @return
	 */
	public static Date set2beEndOfDay(Date time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		return calendar.getTime();
	}
	
	/**
	 * 把时间格式到一天的最后
	 * @param time
	 * @return
	 */
	public static Date set2beStartOfDay(Date time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(time);
		calendar.set(Calendar.HOUR_OF_DAY, 00);
		calendar.set(Calendar.MINUTE, 00);
		calendar.set(Calendar.SECOND, 00);
		return calendar.getTime();
	}
	
	/**
	 * 计算两个时间之间的天数
	 * @param later
	 * @param former
	 * @return
	 */
	public static Long getDistanceBetween2days(Date later, Date former) {
		later = set2beEndOfDay(later);
		former = set2beEndOfDay(former);

		Calendar calendar1 = Calendar.getInstance();
		Calendar calendar2 = Calendar.getInstance();
		calendar1.setTime(later);
		calendar2.setTime(former);

		Long t1 = calendar1.getTimeInMillis();
		Long t2 = calendar2.getTimeInMillis();
		int a = 24 * 60 * 60 * 1000;
		BigDecimal b1 = new BigDecimal(t1.toString());
		BigDecimal b2 = new BigDecimal(t2.toString());
		BigDecimal ba = new BigDecimal(String.valueOf(a));

		BigDecimal distance = b1.subtract(b2).divide(ba, RoundingMode.HALF_UP);
		if (distance.compareTo(BigDecimal.ZERO) > 0
				&& distance.compareTo(BigDecimal.ONE) < 0)
			return 1L;
		if (distance.compareTo(BigDecimal.ONE.negate()) > 0
				&& distance.compareTo(BigDecimal.ZERO) < 0)
			return -1L;

		// +1
		Long val = distance.longValue();
		if (distance.compareTo(new BigDecimal(val.toString())) > 0)
			val += 1;
		return val;
	}
	/**
	 * 判断时辰
	 * @param now
	 * @return
	 */
	public static String getTimeName(Date now){
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(now);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		if(hour >= 0 && hour < 6)
			return "凌晨";
		else if(hour >= 6 && hour < 11)
			return "上午";
		else if(hour >= 11 && hour < 14)
			return "中午";
		else if(hour >= 14 && hour < 18)
			return "下午";
		else
			return "晚上";
	}

	
	public static void main(String[] args) {
	System.out.println(getDateString(new Date(),  "yyyy-MM-dd hh:mm:ss"));
	System.out.println(getRemainSeconds(getDate(new Date(),  "yyyy-MM-dd hh:mm:ss"), getDate(new Date(),  "yyyy-MM-dd hh:mm:ss")));
	}
}
